let Menu = require('Menu')
let UserDataManager = require('UserDataManager')
let WX = require('WX')

let UnlockCarBoard = cc.Class({
  extends: cc.Component,

  properties: {
    car: cc.Node,
    noThank: cc.Node,

  },

  statics: {
    instance: null
  },

  ctor () {
    if (!UnlockCarBoard.instance) {
      UnlockCarBoard.instance = this
    }
  },

  onLoad () {},

  start () {},

  unlock () {
    let nextChapter = () => {
      UserDataManager.saveUserData2(
        'dollar',
        UserDataManager.getUserData().dollar + this.chapterExp
      )
      UserDataManager.saveUserData2('carLevel', this.carLevel + 1)
      this.toMenu()
    }

    WX.watchVideoAD(() => {
      nextChapter()
    })
  },

  open (args) {
    /*
        params
            args {carLevel, chapterExp}
    */
    WX.showBannerAd(() => {
      this.noThank.y += 100
    })
    this.node.opacity = 0
    this.node.active = true
    this.node.runAction(cc.fadeTo(1.5, 255))

    this.chapterExp = args.chapterExp
    if (this.carLevel) {
      this.car.getChildByName('car_' + this.carLevel).active = false
    }
    this.carLevel = args.carLevel
    this.car.getChildByName('car_' + args.carLevel).active = true
    this.car.removeComponent(cc.RigidBody)
    this.car.removeComponent(cc.PhysicsPolygonCollider)
    this.car.removeComponent(cc.BoxCollider)
  },

  close () {
      this.toMenu();
  },

  toMenu () {
    this.noThank.y -= 100
    this.node.active = false
    let _Main = this.node.parent.getComponent('Main')
    _Main.clearAllSceneObjects()
    _Main.clearAllSpecial()
    _Main.updateBoard()
    Menu.instance.open()
  },

  update (dt) {}
})
